package com.winit.core.server.factory;

import com.winit.common.util.ConfigUtil;
import com.winit.common.util.Constants;
import com.winit.deploy.server.WinitJumpServer;
import com.winit.jumpServer.bean.JumpServerInfo;
import com.winit.jumpServer.util.JumpServerListInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * 生成跳板机server对象
 * Created by wangjian on 16/9/23.
 */
public class LinuxJumpServerFactory {

    private static final Logger logger = LoggerFactory.getLogger(LinuxJumpServerFactory.class);

    private static final LinuxJumpServerFactory JUMP_SERVER_FACTORY = new LinuxJumpServerFactory();

    private LinuxJumpServerFactory() {
    }

    public static LinuxJumpServerFactory getInstance() {
        return JUMP_SERVER_FACTORY;
    }

    public void reloadJumpServer() throws Exception {
        logger.info("重置跳板机对象.");
        for (String key : JumpServerListInfo.linuxJumpServer.keySet()) {
            WinitJumpServer server = JumpServerListInfo.linuxJumpServer.get(key);
            if (server != null) {
                server.disConnect();
                JumpServerListInfo.linuxJumpServer.put(key, null);
            }
        }
        initJumpServer();
    }

    //获取上级跳板机对象
    public WinitJumpServer getParentServer(String ip) throws Exception {
        JumpServerInfo info = JumpServerListInfo.jumpServerInfo.get(ip);
        WinitJumpServer server = JumpServerListInfo.linuxJumpServer.get(ip);
        if (!StringUtils.isEmpty(info.getParentIp())) {
            server = getJumpServerLevelOne(info.getParentIp());
        }
        return server;
    }


    public WinitJumpServer getJumpServer(String ip) throws Exception {
        WinitJumpServer server = JumpServerListInfo.linuxJumpServer.get(ip);
        if (server != null && server.isConnected()) {
            logger.info("获取跳板机对象ip=" + ip + ",port=" + server);
            return server;
        }
        JumpServerInfo info = JumpServerListInfo.jumpServerInfo.get(ip);
        if ("1".equals(info.getLevel())) {
            server = getJumpServerLevelOne(info.getIp());
        } else if ("2".equals(info.getLevel())) {
            server = getJumpServerLevelTwo(info.getIp());
        }
        return server;
    }


    //初始化所有跳板机对象
    public void initJumpServer() throws Exception {
        for (String key : JumpServerListInfo.jumpServerInfo.keySet()) {
            getJumpServer(key);
        }
    }

    /**
     * 获取一级跳板机对象
     *
     * @return
     */
    private WinitJumpServer getJumpServerLevelOne(String ip) throws Exception {
        WinitJumpServer server = JumpServerListInfo.linuxJumpServer.get(ip);
        if (server != null && server.isConnected()) {
            logger.info("获取一级跳板机对象,port=" + server);
            return server;
        }
        JumpServerInfo info = JumpServerListInfo.jumpServerInfo.get(ip);
        server = new WinitJumpServer(info.getIp(), ConfigUtil.getValue("ssh_login_user")
                , Integer.parseInt(info.getSshPort()), info.getLevel(), info.getParentIp());
        StringBuffer keyFilePath = new StringBuffer();
        keyFilePath.append("/home/").append(ConfigUtil.getValue("ssh_login_user")).append("/").append(ConfigUtil.getValue("ssh_key_file"));
        server.addPrivateKeyPath(keyFilePath.toString());
        server.setConnectionTimeout(Constants.SSH_CONNECT_TIMEOUT);
        server.connect();
        JumpServerListInfo.linuxJumpServer.put(ip, server);
        logger.info("获取一级跳板机对象,port=" + server);
        return server;
    }

    /**
     * 获取二级跳板机对象
     *
     * @return
     */
    private WinitJumpServer getJumpServerLevelTwo(String ip) throws Exception {
        WinitJumpServer server = JumpServerListInfo.linuxJumpServer.get(ip);
        if (server != null && server.isConnected()) {
            logger.info("获取二级跳板机对象,port=" + server);
            return server;
        }
        JumpServerInfo info = JumpServerListInfo.jumpServerInfo.get(ip);
        WinitJumpServer oneLevelJumpServer = getJumpServerLevelOne(info.getParentIp());
        server = new WinitJumpServer(info.getIp(), ConfigUtil.getValue("ssh_login_user")
                , Integer.parseInt(info.getSshPort()), info.getLevel(), info.getParentIp());
        StringBuffer keyFilePath = new StringBuffer();
        keyFilePath.append("/home/").append(ConfigUtil.getValue("ssh_login_user")).append("/").append(ConfigUtil.getValue("ssh_key_file"));
        server.addPrivateKeyPath(keyFilePath.toString());
        server.setConnectionTimeout(Constants.SSH_CONNECT_TIMEOUT);
        oneLevelJumpServer.jump(server);
        server.connect();
        JumpServerListInfo.linuxJumpServer.put(ip, server);
        logger.info("获取二级跳板机对象,port=" + server);
        return server;
    }
}
